package org.techism.client;

import java.util.List;
import java.util.Map;
import java.util.SortedMap;

import org.techism.client.bo.Event;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

public class EventPanelView extends PanelView {

	/**
	 * Create a remote service proxy to talk to the server-side Group Service
	 */
	private final EventServiceAsync eventService = GWT.create(EventService.class);

	final HorizontalPanel tagPanel;
	final FlowPanel eventPanel;

	public EventPanelView(final FlowPanel hPanel) {
		tagPanel = new HorizontalPanel();
		hPanel.add(tagPanel);
		eventPanel = new FlowPanel();
		hPanel.add(eventPanel);
	}

	public void addEventTags() {
		tagPanel.clear();
		eventService.getTags(new AsyncCallback<SortedMap<String, Integer>>() {
			public void onFailure(final Throwable caught) {
				showError();
			}

			public void onSuccess(final SortedMap<String, Integer> result) {
				final ClickHandler handler = new ClickHandler() {
					public void onClick(final ClickEvent event) {
						Anchor tag = (Anchor) event.getSource();
						final String text = tag.getText();
						if (text.equals("Alles anzeigen")) {
							selectEvents(null);
						} else {
							selectEvents(text);
						}
					}
				};
				TagPanelHelper.fillTagPanel(result, handler, tagPanel);
			}
		});
	}

	public void addEvents() {
		eventService.getFutureEventsForThisYear(new AsyncCallback<SortedMap<String, List<Event>>>() {
			public void onFailure(final Throwable caught) {
				showError();
			}

			public void onSuccess(final SortedMap<String, List<Event>> result) {
				processEvents(result);
			}
		});
	}

	private void selectEvents(final String tag) {
		eventPanel.clear();
		eventService.getFutureEventsForThisYear(tag, new AsyncCallback<SortedMap<String, List<Event>>>() {
			public void onFailure(final Throwable caught) {
				showError();
			}

			public void onSuccess(final SortedMap<String, List<Event>> result) {
				processEvents(result);
			}
		});
	}

	private void processEvents(final Map<String, List<Event>> result) {
		for (final String eventDate : result.keySet()) {
			eventPanel.add(new HTML("<h2>" + eventDate + "</h2>"));
			for (final Event event : result.get(eventDate)) {
				final Label title = new Label(event.getTitle());
				title.addStyleDependentName("title");
				eventPanel.add(title);
				eventPanel.add(new Label(event.getOrganizer()));
				final Anchor anchor = new Anchor(event.getUrl(), event.getUrl());
				eventPanel.add(anchor);
				String tagString = "";
				for (final String tag : event.getTags()) {
					tagString += tag + ", ";
				}
				eventPanel.add(new Label(tagString));
				eventPanel.add(new HTML("<br/>"));
			}
		}
	}
}
